package misty.mispic.app.ui.photo

import com.google.common.eventbus.Subscribe
import com.sun.javafx.css.StyleManager
import de.jensd.fx.fontawesome.AwesomeDude
import de.jensd.fx.fontawesome.AwesomeIcon
import groovy.transform.CompileStatic
import javafx.beans.value.ChangeListener
import javafx.event.EventHandler
import javafx.event.WeakEventHandler
import javafx.fxml.FXML
import javafx.fxml.Initializable
import javafx.scene.Scene
import javafx.scene.control.*
import javafx.scene.image.Image
import javafx.scene.image.ImageView
import javafx.scene.input.KeyCode
import javafx.scene.input.KeyEvent
import javafx.scene.input.ScrollEvent
import misty.mispic.app.event.EventBus
import misty.mispic.app.event.PhotoEvent
import misty.mispic.app.event.ThumbEvent
import misty.mispic.app.ui.CSS

/**
 * Created by Misty on 2015/1/25.
 */
@CompileStatic
class Photo extends Control implements Initializable {
    static {
        StyleManager.instance.addUserAgentStylesheet(CSS.photo.location)
    }

    @FXML
    private ListView mThumbs

    @FXML
    private ScrollPane mImageContainer

    @FXML
    private Label mImageLabel

    @FXML
    private ImageView mPhoto

    @FXML
    private Button mClose

    private EventHandler keyPressHandler = { KeyEvent e ->
        if (e.code == KeyCode.RIGHT) {
            EventBus.post(new ThumbEvent.Next())
            e.consume()
        } else if (e.code == KeyCode.LEFT) {
            EventBus.post(new ThumbEvent.Prev())
            e.consume()
        }
    } as EventHandler

    private WeakEventHandler weakKeyPressHandler = new WeakEventHandler(keyPressHandler)

    Photo() {
        sceneProperty().addListener({ obs, Scene ov, Scene nv ->
            if (ov) {
                EventBus.unregister(this)
            }
            if (nv) {
                EventBus.register(this)
            }
        } as ChangeListener)
        getStyleClass().add("photo")
    }

    @Override
    void initialize(URL location, ResourceBundle resources) {
        setVisible(false)

        final def node = mImageLabel
        node.onScroll = { ScrollEvent e ->
            double delta = 0.1
            if (e.deltaY < 0) {
                delta = -delta
            }
            def sx = Math.max(node.scaleX + delta, 0.5)
            def sy = Math.max(node.scaleY + delta, 0.5)
            node.scaleX = sx
            node.scaleY = sy
        } as EventHandler

        AwesomeDude.setIcon(mClose, AwesomeIcon.CLOSE, "18", ContentDisplay.GRAPHIC_ONLY)
        mClose.onAction = {
            setVisible(false)
        }

        addEventHandler(KeyEvent.KEY_PRESSED, weakKeyPressHandler)
        mImageContainer.addEventHandler(KeyEvent.KEY_PRESSED, weakKeyPressHandler)
    }

    @Override
    protected Skin createDefaultSkin() {
        return new PhotoSkin(this)
    }

    @Subscribe
    void showPhoto(PhotoEvent.Show show) {
        println "show photo ${show.thumb}"
        setVisible(true)
        requestFocus()

        def url = show.thumb.impl_getUrl()
        def img = new Image(url)

        mPhoto.setImage(img)
    }
}
